<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>98_99数组去重</title>
		<script>
			/* 
			有如下一个数组
			arr = [1,2,1,3,2,4,5,5,6,7]
			编写代码，去除数组中重复的元素 --> [1,2,3,4,5,6,7]
			 */
			function removeRepeat(arr) {
				let isArray = arr instanceof Array;
				if (!isArray) {
					alert("请输入数组")
					return null;
				}
				let tempArr = [];
				for (let j = 0; j < arr.length; j++) {
					tempArr.push(arr[j]);
					for (let i = j + 1; i < arr.length; i++) {
						let equal = false;
						if (arr[j] == arr[i]) {
							equal = true;
						}
						if (equal) {
							tempArr.pop(arr[j])
						}
					}
				}
				return tempArr;
			}

			function removeRepeat2(arr) {
				let isArray = arr instanceof Array;
				if (!isArray) {
					alert("请输入数组")
					return null;
				}
				for (let j = 0; j < arr.length; j++) {
					for (let i = j + 1; i < arr.length; i++) {
						if (arr[j] == arr[i]) {
							arr.splice(i, 1)
							j--;
						}
					}
				}
				return arr;
			}


			function removeRepeat3(arr) {
				for (let i = 0; i < arr.length; i++) {
					let index = arr.indexOf(arr[i], i + 1);
					if (index != -1) {
						arr.splice(index, 1);
						i--;
					}
				}
				return arr;
			}


			function removeRepeat4(arr) {
				const newArr = []
				for (let ele of arr) {
					if (newArr.indexOf(ele) === -1) {
						newArr.push(ele)
					}
				}
				return newArr;
			}

			let arr = [1, 2, 1, 3, 2, 4, 5, 5, 5, 5, 6, 7];
			let ret = removeRepeat4(arr)
			console.log("ret=", ret);
		</script>
	</head>
	<body>
	</body>
</html>